import time
import tidevice
from PyQt5.QtCore import QThread, pyqtSignal
from mier_sdk.work_func import get_uuids


class Connect_Manager(QThread):
    '''
        xinhaocao
    '''
    my_signal_device_ios = pyqtSignal(list)
    erro_connect = pyqtSignal(list)

    def __init__(self,ui):
        super().__init__()
        self.ui = ui
        self.q = self.ui.q

    def run(self) -> None:
        while 1 :
            try:
                list_ios = get_uuids()
                for ios_device in list_ios:
                    if ios_device not in self.ui.devices_ios:
                        try:
                            #添加设备
                            print('添加设备')
                            out = tidevice.Device(ios_device).device_info()
                            name = out['DeviceName']
                            self.ui.devices_ios.add(ios_device)
                            self.my_signal_device_ios.emit([ios_device,name])
                        except Exception as e:
                            self.erro_connect.emit([ios_device,3,'连接报错:重启手机或者插紧数据线后重试'])
                            print('连接iOS设备报错：', e)
                time.sleep(0.3)
                for udid in self.q.keys():
                    if not self.q[udid].empty() :
                        value = self.q[udid].get(True)
                        if len(value) > 0  :
                            self.erro_connect.emit(value)
            except Exception as e:
                print('连接iOS设备报错：', e)







